<?php

namespace App\Model;

use App\Dao\BillDao;
use App\Dao\ShopsDao;
use App\Dao\UsersDao;
use Illuminate\Support\Facades\DB;

class DouyinOrderModel extends BaseModel
{
    protected $table = 'douyin_orders';
    protected $primaryKey = 'douyin_order_id';
    protected $fillable = [
        'order_amount',
        'order_account_id',
        'status',
        'order_sanfang_id',
    ];

    public static function changeData($order_id)
    {
        $info = self::query()->where('order_sanfang_id',$order_id)->first();
        if($info['status'] != 2){
            return;
        }
        $shopInfo = ShopsDao::get_builder_query([
            'shop_laike_id'=>$info['order_account_id']
        ]);
        if($shopInfo['user_daili_id'] == 0){
            return;
        }
        //查询销售奖励
        $userInfo = UsersDao::get_user_builder([
            'user_id'=>$shopInfo['user_daili_id']
        ]);
        $point_uid_spread_price_id = $userInfo['point_uid_spread_price_id'];
        DB::beginTransaction();
        $res = true;
        $moneyAll = bcmul($info['order_amount'],$shopInfo['shop_value_yong'],2);
        if($point_uid_spread_price_id != 0) {
            $res !== false && $res = BillDao::get_bill_builder()->create([
                'user_id' => $point_uid_spread_price_id,
                'pm' => 1,
                'type' => 1,
                'num' => bcmul($moneyAll,$shopInfo['xiaoshoujiang'],2),
                'title' => '抖音店铺核销券返还总监销售将'
            ]);
            $res !== false && $res = self::get_user_builder([
                'user_id' => $point_uid_spread_price_id
            ])->increment('k_price', bcmul($moneyAll,$shopInfo['xiaoshoujiang'],2));
            $df_fan = bcmul($moneyAll,$shopInfo['xiaoshoujiang'],2);
            if (UsersDao::get_user_builder([
                    'user_id' => $point_uid_spread_price_id
                ])->value('d_price') < $df_fan) {
                $df_fan = UsersDao::get_user_builder([
                    'user_id' => $point_uid_spread_price_id
                ])->value('d_price');
            }
            if($df_fan > 0) {

                $res !== false && $res = BillDao::get_bill_builder()->create([
                    'user_id' => $point_uid_spread_price_id,
                    'num' => $df_fan,
                    'pm' => 0,
                    'type' => 2,
                    'title' => '用户释放DF值'
                ]);
                $res !== false && $res = UsersDao::get_user_builder([
                    'user_id'=>$point_uid_spread_price_id
                ])->decrement('d_price',$df_fan);
            }
        }
        $point_uid_spread_price_info = UsersDao::get_user_builder([
            'user_id'=>$point_uid_spread_price_id
        ])->first();
        if($shopInfo['point_uid_price'] > 0 && $point_uid_spread_price_info['point_uid'] != 0){
            //管理奖励总监的节点人
            $res !== false && $res = BillDao::get_bill_builder()->create([
                'user_id'=>$point_uid_spread_price_info['point_uid'],
                'pm'=>1,
                'type'=>1,
                'num'=>bcmul($shopInfo['point_uid_price'],$moneyAll,2),
                'title'=>'抖音核销券管理奖励'
            ]);
            $res !== false && $res = self::get_user_builder([
                'user_id'=>$point_uid_spread_price_info['point_uid']
            ])->increment('k_price',bcmul($shopInfo['point_uid_price'],$moneyAll,2));

            $res !== false && $res = self::get_user_builder([
                'user_id'=>$point_uid_spread_price_info['point_uid']
            ])->increment('all_k_price',bcmul($shopInfo['point_uid_price'],$moneyAll,2));


            $df_fan = bcmul($shopInfo['point_uid_price'],$moneyAll,2);
            if(UsersDao::get_user_builder([
                    'user_id'=>$point_uid_spread_price_info['point_uid']
                ])->value('d_price') < $df_fan){
                $df_fan = UsersDao::get_user_builder([
                    'user_id'=>$point_uid_spread_price_info['point_uid']
                ])->value('d_price');
            }
            if($df_fan > 0) {

                $res !== false && $res = BillDao::get_bill_builder()->create([
                    'user_id' => $point_uid_spread_price_info['point_uid'],
                    'num' => $df_fan,
                    'pm' => 0,
                    'type' => 2,
                    'title' => '用户释放DF值'
                ]);
                $res !== false && $res = UsersDao::get_user_builder([
                    'user_id'=>$point_uid_spread_price_info['point_uid']
                ])->decrement('d_price',$df_fan);
            }


            $point_uid_spread_price_info = UsersDao::get_user_builder([
                'user_id'=>$point_uid_spread_price_info['point_uid']
            ]);
            if($point_uid_spread_price_info->exists()){
                $point_uid_spread_price_info = $point_uid_spread_price_info->first()->toArray();
                if($point_uid_spread_price_info['point_uid'] != 0 && bcmul($shopInfo['point_uid_two_price'],$moneyAll,2) > 0){
                    $res !== false && $res = BillDao::get_bill_builder()->create([
                        'user_id'=>$point_uid_spread_price_info['point_uid'],
                        'pm'=>1,
                        'type'=>1,
                        'num'=>bcmul($shopInfo['point_uid_two_price'],$moneyAll,2),
                        'title'=>'抖音核销券管理间推奖励'
                    ]);

                    $res !== false && $res = self::get_user_builder([
                        'user_id'=>$point_uid_spread_price_info['point_uid']
                    ])->increment('k_price',bcmul($shopInfo['point_uid_two_price'],$moneyAll,2));

                    $res !== false && $res = self::get_user_builder([
                        'user_id'=>$point_uid_spread_price_info['point_uid']
                    ])->increment('all_k_price',bcmul($shopInfo['point_uid_two_price'],$moneyAll,2));





                    $df_fan = bcmul($shopInfo['point_uid_two_price'],$moneyAll,2);
                    if(UsersDao::get_user_builder([
                            'user_id'=>$point_uid_spread_price_info['point_uid']
                        ])->value('d_price') < $df_fan){
                        $df_fan = UsersDao::get_user_builder([
                            'user_id'=>$point_uid_spread_price_info['point_uid']
                        ])->value('d_price');
                    }
                    if($df_fan > 0) {
                        $res !== false && $res = BillDao::get_bill_builder()->create([
                            'user_id' => $point_uid_spread_price_info['point_uid'],
                            'num' => $df_fan,
                            'pm' => 0,
                            'type' => 2,
                            'title' => '用户释放DF值'
                        ]);
                        $res !== false && $res = UsersDao::get_user_builder([
                            'user_id' => $point_uid_spread_price_info['point_uid']
                        ])->decrement('d_price', $df_fan);
                    }
                }
            }
        }

        $res !== false && $res = self::query()->where('order_sanfang_id',$order_id)->update([
            'status'=>3
        ]);

        if($res === false){
            DB::rollBack();
            self::error('放置失败');
        }
        DB::commit();
    }
}
